<template>
  <div class="awsCenter">
    <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick" style="width: 100%">
      <el-tab-pane label="用户" name="client">
        <div style="border: black 0px solid;width: 100%;height: 550px;display: flex;
flex-direction: column;
align-items: center;">
            <!--    按钮栏    -->
            <div class="awsCenter" style="width: 100%;height: 40px;border: black 0px solid">
              <el-input style="width: 200px;margin-right: 10px" placeholder="姓名" v-model="realName"></el-input>
              <el-select clearable v-model="gender" placeholder="性别" style="width: 100px">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <div class="demo-date-picker" style="margin-left: 10px">
                <div class="block">
                  <el-date-picker
                    v-model="dateTime"
                    type="daterange"
                    range-separator="~"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期"
                  />
                </div>
              </div>
              <el-button @click="getClient" style="width: 80px;margin-left: 10px" type="primary" plain>查询</el-button>
              <el-button style="width: 80px;margin-left: auto" type="primary" @click="showSaveClient=true;sou=false;dialogTitle='新增用户信息'">新增</el-button>
              <el-button @click="confDelete">批量删除</el-button>

            </div>

          <!--   表格    -->
          <el-table :data="tableData" stripe style="width: 100%;margin-top: 20px" @selection-change="selectClient">
            <el-table-column type="selection" :selectable="selectable" width="55" />
            <el-table-column prop="username" label="账号" width="150" />
<!--            <el-table-column prop="nickname" label="昵称" width="100" />-->
            <el-table-column prop="realname" label="姓名" width="100" />
            <el-table-column prop="gender" label="性别" width="100px"/>
            <el-table-column prop="email" label="邮箱" width="180px"/>
            <el-table-column prop="phone" label="手机号" />
            <el-table-column prop="birthday" label="生日" width="150px"/>
            <el-table-column prop="address" label="住址" />
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button type="primary" plain :icon="Edit" circle @click="sou=true;showSaveClient=true;handleUpdate(row);dialogTitle='修改用户信息'" />
                <el-button type="danger" plain :icon="Delete" circle @click="handleClientDelete(row)" />
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页条 -->
          <div  class="demo-pagination-block">
            <el-pagination
              style="margin-top: 20px"
              v-model:current-page="currentPage"
              :page-size="8"
              :size="size"
              :disabled="disabled"
              :background="background"
              layout="total, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>

        </div>
      </el-tab-pane>
      <el-tab-pane label="管理员" name="admin">
        <div style="border: black 0px solid;width: 100%;height: 550px;display: flex;
flex-direction: column;
align-items: center;">
          <!--    按钮栏    -->
          <div class="awsCenter" style="width: 100%;height: 40px;border: black 0px solid">
            <el-input style="width: 200px;margin-right: 10px" placeholder="姓名" v-model="realName"></el-input>
            <el-select clearable v-model="gender" placeholder="性别" style="width: 100px">
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <div class="demo-date-picker" style="margin-left: 10px">
              <div class="block">
                <el-date-picker
                  v-model="dateTime"
                  type="daterange"
                  range-separator="~"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                />
              </div>
            </div>
            <el-button @click="getClient" style="width: 80px;margin-left: 10px" type="primary" plain>查询</el-button>
            <el-button style="width: 80px;margin-left: auto" type="primary" @click="showSaveClient=true;sou=false;dialogTitle='新增管理员'">新增</el-button>
            <el-button @click="confDelete">批量删除</el-button>

          </div>

          <!--   表格    -->
          <el-table :data="tableData" stripe style="width: 100%;margin-top: 20px" @selection-change="selectClient">
            <el-table-column type="selection" :selectable="selectable" width="55" />
            <el-table-column prop="username" label="账号" width="150" />
            <!--            <el-table-column prop="nickname" label="昵称" width="100" />-->
            <el-table-column prop="realname" label="姓名" width="100" />
            <el-table-column prop="gender" label="性别" width="100px"/>
            <el-table-column prop="email" label="邮箱" width="180px"/>
            <el-table-column prop="phone" label="手机号" />
            <el-table-column prop="birthday" label="生日" width="150px"/>
            <el-table-column prop="address" label="住址" />
            <el-table-column label="操作">
              <template #default="{ row }">
                <el-button type="primary" plain :icon="Edit" circle @click="sou=true;showSaveClient=true;handleUpdate(row);dialogTitle='修改管理员'" />
                <el-button type="danger" plain :icon="Delete" circle :disabled="row.disabled" @click="handleClientDelete(row)" />
              </template>
            </el-table-column>
          </el-table>
          <!-- 分页条 -->
          <div  class="demo-pagination-block">
            <el-pagination
              style="margin-top: 20px"
              v-model:current-page="currentPage"
              :page-size="8"
              :size="size"
              :disabled="disabled"
              :background="background"
              layout="total, prev, pager, next"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>

        </div>
      </el-tab-pane>
    </el-tabs>

    <el-dialog
      @close="cleanClientForm"
      v-model="showSaveClient"
      :title="dialogTitle"
      width="500"
      align-center
      style="height: 500px; /* 固定高度 */
          "
      draggable
      :close-on-click-modal="false"
    >

      <div style="padding: 20px;height: 400px">
        <el-form :model="clientForm" label-position="top" :rules="rules" label-width="100px">

          <el-tabs v-model="saveClientClass">
            <el-tab-pane label="账号信息" name="accountInfo">
              <el-form-item prop="username" label-position="top" label="账号">
                <el-input style="width: 200px" v-model="clientForm.username"></el-input>
              </el-form-item>
              <el-form-item prop="password"  label-position="top" label="密码">
                <el-input style="width: 200px" show-password type="password" v-model="clientForm.password"></el-input>
              </el-form-item>
              <el-form-item prop="confPassword" label-position="top" label="确认密码">
                <el-input style="width: 200px" show-password type="password" v-model="confPassword"></el-input>
              </el-form-item>
            </el-tab-pane>
            <el-tab-pane label="个人信息" name="personInfo">
              <div style="display: flex;flex-direction: row">
                <div>
                  <el-form-item label-position="top" label="昵称">
                    <el-input style="width: 150px" v-model="clientForm.nickname"></el-input>
                  </el-form-item>
                  <el-form-item label-position="top" label="真实姓名">
                    <el-input style="width: 150px" v-model="clientForm.realname"></el-input>
                  </el-form-item>
                </div>
                <div style="margin-left: 100px">
                  <el-form-item label-position="top" label="头像">
                    <el-upload
                      class="avatar-uploader"
                      :action="UploadUrl()"
                      :show-file-list="false"
                      accept=".jpg,.png"
                      @change="onFileChange"
                      :before-upload="beforeAvatarUpload"
                      :http-request="uploadHttpRequest"
                    >
                      <img style="height: 110px" v-if="imageUrl" :src="imageUrl" class="avatar" />
                      <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                    </el-upload>
                  </el-form-item>
                </div>

              </div>
              <el-form-item label="性别">
                <el-radio-group v-model="clientForm.gender">
                  <el-radio label="男">男</el-radio>
                  <el-radio label="女">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="出生年月">
                <el-date-picker
                  v-model="clientForm.birthday"
                  type="date"
                  placeholder="选择日期"
                >
                </el-date-picker>
              </el-form-item>
            </el-tab-pane>

            <el-tab-pane label="联系方式" name="numInfo">
              <el-form-item label="邮箱">
                <el-input style="width: 200px" v-model="clientForm.email"></el-input>
              </el-form-item>
              <el-form-item label="电话">
                <el-input style="width: 200px" v-model="clientForm.phone"></el-input>
              </el-form-item>

              <el-form-item label="地址" >
                <el-input type="textarea" style="width: 400px" v-model="clientForm.address"></el-input>
              </el-form-item>
            </el-tab-pane>
          </el-tabs>

        </el-form>
      </div>

      <template #footer>
        <div style="margin-top: -20px">
          <el-button @click="showSaveClient = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdate">
            确 认
          </el-button>
        </div>
      </template>
    </el-dialog>


    <el-dialog v-model="confDialog" :close-on-click-modal="false" title="是否确认删除 ?" width="400" center>
      <template #footer>
        <div class="dialog-footer">
          <el-button style="margin-right: 10px" @click="confDialog = false">取 消</el-button>
          <el-button style="margin-right: 20px" type="primary" @click="confDeleteDialog">
            确 定
          </el-button>
        </div>
      </template>
    </el-dialog>

    <el-dialog
      @close="cleanExamine"
      v-model="showExamine"
      style="height: 610px"
      title="咨询师信息"
      width="850"
      align-center
      :close-on-click-modal="false"
      :show-close="true"
    >
      <el-form style="height: 500px;overflow: auto;" :model="counselorForm" label-position="top" label-width="100px">

        <el-tabs v-model="examineClass" class="demo-tabs" @tab-click="changeExamine" style="width: 100%">
          <el-tab-pane label="基本信息" name="1">
            <div style="display: flex;flex-direction: row">
              <div>
                <el-form-item prop="nickname" label-position="top" label="昵称">
                  <el-input :disabled="isDisabled" style="width: 200px" placeholder="请输入" v-model="userForm.nickname"></el-input>
                </el-form-item>

                <el-form-item prop="realname" label-position="top" label="姓名">
                  <el-input :disabled="isDisabled" style="width: 200px" placeholder="请输入" v-model="userForm.realname"></el-input>
                </el-form-item>
                <el-form-item prop="gender" label-position="top" label="性别">
                  <el-radio-group :disabled="isDisabled" v-model="userForm.gender">
                    <el-radio value="男">男</el-radio>
                    <el-radio value="女">女</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item  prop="phone" label-position="top" label="电话">
                  <el-input :disabled="isDisabled" style="width: 200px" placeholder="请输入" v-model="userForm.phone"></el-input>
                </el-form-item>
              </div>
              <div style="margin-left: 250px">
                <el-form-item  label-position="top" label="头像">
                  <el-upload
                    class="avatar-uploader"
                    :action="UploadUrl()"
                    :show-file-list="false"
                    accept=".jpg,.png"
                    @change="onFileChange"
                    :disabled="isDisabled"
                  >
                    <img style="height: 110px" v-if="examineImageUrl" :src="examineImageUrl" class="avatar" />
                    <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
                  </el-upload>
                </el-form-item>
                <el-form-item label="出生年月">
                  <el-date-picker :disabled="isDisabled"
                                  v-model="userForm.birthday"
                                  type="date"
                                  placeholder="选择日期"
                  >
                  </el-date-picker>
                </el-form-item>
                <el-form-item label="邮箱">
                  <el-input :disabled="isDisabled" style="width: 220px" v-model="userForm.email"></el-input>
                </el-form-item>

              </div>
            </div>
            <el-form-item label="地址" >
              <el-input :disabled="isDisabled" type="textarea" style="width: 400px" v-model="userForm.address"></el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="咨询师信息" name="2">
            <el-form-item style="margin-top: 50px" prop="specialty" label-position="top" label="选择专业领域">
              <el-select :disabled="isDisabled" v-model="counselorForm.specialty" placeholder="请选择" style="width: 240px">
              </el-select>
            </el-form-item>

            <el-form-item prop="bio" label-position="top" label="个人简介">
              <el-input :disabled="isDisabled" v-model="counselorForm.bio" placeholder="请输入" style="width: 800px"
                        :rows="8"
                        type="textarea" >

              </el-input>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="工作时间" name="3">
            <el-table :data="timeRows" style="width: 100%;height: 400px;overflow-y: auto;">
              <el-table-column width="180" label="咨询室">
                <template v-slot="{ row }">
                  <el-select :disabled="isDisabled" v-model="row.room" placeholder="请选择" style="width: 150px">

                  </el-select>
                </template>
              </el-table-column>
              <el-table-column width="180" label="工作日">
                <template v-slot="{ row }">
                  <el-select :disabled="isDisabled" v-model="row.weekend" placeholder="请选择" style="width: 150px">

                  </el-select>
                </template>
              </el-table-column>
              <el-table-column width="300" label="工作时间">
                <template v-slot="{ row }">
                  <div style="display: flex;flex-direction: row">
                    <div>
                      <el-time-select :disabled="isDisabled"
                                      format="HH:mm:ss"
                                      v-model="row.starttime"
                                      style="width: 130px"
                                      :max-time="row.endtime"
                                      class="mr-4"
                                      placeholder="开始时间"
                                      start="08:30"
                                      step="00:15"
                                      end="18:30"
                      />
                    </div>
                    <div style="margin-left: 10px">
                      <el-time-select :disabled="isDisabled"
                                      format="HH:mm:ss"
                                      v-model="row.endtime"
                                      style="width: 130px"
                                      :min-time="row.starttime"
                                      placeholder="结束时间"
                                      start="08:30"
                                      step="00:15"
                                      end="18:30"
                      />
                    </div>
                  </div>

                </template>
              </el-table-column>
              <el-table-column label="操作">
                <template v-slot="{ row, $index }">
                  <el-button :disabled="isDisabled"  type="danger" circle plain :icon="Delete" @click="deleteRow($index)"></el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
        </el-tabs>
      </el-form>

      <!--   按钮栏目     -->
      <div  style="border-top: 1px solid #D3D3D3;padding-top: 10px">
          <el-button style="margin-left: 85%" @click="filed(counselorForm.username)" type="primary">审 批</el-button>
      </div>

    </el-dialog>

    <el-drawer @close="cleanFiledForm" v-model="drawer" title="咨询师信息审核单" :with-header="false">
      <el-form v-model="filedForm" label-position="top" label-width="100px">
        <div style="height: 400px">
          <text style="font-weight: bolder;font-size: 23px">咨询师信息审核单</text>


          <el-form-item style="margin-top: 40px" label="审批结果" prop="approvalResult">
            <el-select v-model="filedForm.approvalresult" style="width: 200px" placeholder="请选择">
              <el-option label="同意" value="同意"></el-option>
              <el-option label="拒绝" value="拒绝"></el-option>
            </el-select>
          </el-form-item>


          <el-form-item style="margin-top: 20px" label="审批备注" prop="approvalNote">
            <el-input type="textarea" style="width: 400px" rows="5" v-model="filedForm.approvalnote"></el-input>
          </el-form-item>
        </div>



        <div style="display: flex;flex-direction: row">
          <div>
            <el-form-item label="审批人" prop="approver">
              <el-input style="width: 200px" v-model="filedForm.approver"></el-input>
            </el-form-item>
          </div>
          <div style="margin-left: 20px">
            <el-form-item label="审批时间" prop="updatedAt">
              <el-date-picker style="width: 210px" v-model="filedForm.updatedat" type="datetime" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </div>

        </div>

        <div style="width: 100%;margin-top: 100px">
          <el-button style="margin-left: auto" @click="approval" plain type="primary">提 交</el-button>
        </div>
      </el-form>
    </el-drawer>

  </div>

</template>
<script lang="ts" setup>
import {getUserClientService,userRegisterService,deleteClientService,updateClientService} from '@/api/user.js'
import {ElMessage, ElMessageBox, UploadFile} from "element-plus";
const confDialog = ref(false)
import moment from 'moment';
import type { ComponentSize } from 'element-plus'
const currentPage = ref(1)
const size = ref<ComponentSize>('default')
const background = ref(false)
const disabled = ref(false)


import { Plus } from '@element-plus/icons-vue'

import type { UploadProps } from 'element-plus'
import {upload} from '@/api/upload.js'

const imageUrl = ref('')

const getStatusType=((status)=>{
  if (status=='未申请'){
    return 'info'
  }else if (status=='已申请'){
    return 'warning'
  }else if (status=='已入驻'){
    return 'success'
  }else {
    return 'error'
  }
})
const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
  if (rawFile.size / 1024 / 1024 > 2) {
    ElMessage.error('头像大小不能超过2MB')
    return false
  }
  return true
}

const UploadUrl = () => {
  // 因为action参数是必填项，我们使用二次确认进行文件上传时，直接填上传文件的url会因为没有参数导致api报404，
  // 所以这里将action设置为一个返回为空的方法就行，避免抛错
  return ""
}

const uploadHttpRequest=((data)=>{

})

const onFileChange=(async (file)=>{
  console.log(file)
  imageUrl.value = URL.createObjectURL(file.raw!)
  const res=await upload(file.raw)
  console.log(res)
  if (res.code===200){
    clientForm.value.avatar=res.message
  }
})


const handleSizeChange = (val: number) => {
  console.log(`${val} items per page`)
}
const handleCurrentChange = async (val: number) => {
  selectData.value = {
    current: val,
    pageSize: 8,
    realname: realName.value,
    gender: gender.value,
    beginDate: dateTime.value[0] ? moment(dateTime.value[0]).format('YYYY-MM-DD') : '',
    endDate: dateTime.value[0] ? moment(dateTime.value[0]).format('YYYY-MM-DD') : ''
  }
  const body = {
    userDto: selectData.value,
    type: userType.value  // 将需要传递的 type 参数赋值给对应的键
  };
  let res = await getUserClientService(body)
  if (res.code === 200) {
    tableData.value = res.data.data
    tableData.value.forEach(item=>{
      if (item.status==0){
        item.status='未申请'
      }else if (item.status==2){
        item.status='已申请'
      }else if (item.status==1){
        item.status='已入驻'
      }else {
        item.status='已拒绝'
      }
    })
    dateFormat()
    total.value = res.data.total
  }
}
import {
  Delete,
  Edit,
} from '@element-plus/icons-vue'
import {onMounted, ref} from 'vue'
import type { TabsPaneContext } from 'element-plus'
const dateTime = ref('')
const realName=ref('')
const activeName = ref('client')

import {useUserStore} from "@/stores/user/user.js";
import { useRouter } from 'vue-router'
import { getByUserIdService } from '@/api/counselor'
const userStore=useUserStore()
const router = useRouter();
onMounted(async ()=>{
  activeName.value=router.currentRoute.value.query.userType?router.currentRoute.value.query.userType:'client'
  userType.value=activeName.value
  const body = {
    userDto: selectData.value,
    type: userType.value  // 将需要传递的 type 参数赋值给对应的键
  };
  //页面挂载渲染表格
  let res=await getUserClientService(body)
  if (res.code===200){

    tableData.value=res.data.data
    dateFormat()
    total.value=res.data.total
    tableData.value.forEach(item => {
      if (item.username===userStore.user.username){
        item.disabled = true
      }else {
        item.disabled = false; // 向每个对象添加 disabled 属性并设置初始值
      }
      if (item.status==0){
        item.status='未申请'
      }else if (item.status==2){
        item.status='已申请'
      }else if (item.status==1){
        item.status='已入驻'
      }else {
        item.status='已拒绝'
      }
    });

  }

})
const gender = ref('')

const options = [
  {
    value: '男',
    label: '男',
  },
  {
    value: '女',
    label: '女',
  }
]

const userType=ref("client")
const dialogTitle=ref('')

//修改选择角色
const buttonDisabled=ref(false)
const handleClick = async (tab: TabsPaneContext, event: Event) => {
  cleanSelectData()
  selected.value=[]
  userType.value=tab.props.name
  console.log(userType.value)
  const body = {
    userDto: selectData.value,
    type: userType.value  // 将需要传递的 type 参数赋值给对应的键
  };
  let res=await getUserClientService(body)
  if (res.code===200){
    tableData.value=res.data.data
    tableData.value.forEach(item => {
      if (item.username===userStore.user.username){
        item.disabled = true
      }else {
        item.disabled = false; // 向每个对象添加 disabled 属性并设置初始值
      }
      if (item.status==0){
        item.status='未申请'
      }else if (item.status==2){
        item.status='已申请'
      }else if (item.status==1){
        item.status='已入驻'
      }else {
        item.status='已拒绝'
      }
    });

    dateFormat()
    total.value=res.data.total
  }
}



const tableData = ref([])

const selectData=ref({
  current:currentPage.value,
  pageSize:8,
  realname:'',
  gender:'',
  beginDate:'',
  endDate:''

})
//查询
const total=ref(0)
const getClient=(async ()=>{

  selectData.value={
    current:currentPage.value,
    pageSize:8,
    realname:realName.value,
    gender:gender.value,
    beginDate:dateTime.value ? moment(dateTime.value[0]).format('YYYY-MM-DD') : null,
    endDate:dateTime.value ? moment(dateTime.value[1]).format('YYYY-MM-DD') : null
  }
  console.log(selectData.value)
  const body = {
    userDto: selectData.value,
    type: userType.value  // 将需要传递的 type 参数赋值给对应的键
  };

  const res=await getUserClientService(body)
  if (res.code===200){
    tableData.value=res.data.data
    tableData.value.forEach(item => {
      if (item.username===userStore.user.username){
        item.disabled = true
      }else {
        item.disabled = false; // 向每个对象添加 disabled 属性并设置初始值

      }
      if (item.status==0){
        item.status='未申请'
      }else if (item.status==2){
        item.status='已申请'
      }else if (item.status==1){
        item.status='已入驻'
      }else {
        item.status='已拒绝'
      }

    });
    dateFormat()
    total.value=res.data.total
    console.log(tableData.value)
  }
})

const cleanSelectData=(()=>{
  selectData.value={
    current:currentPage.value,
    pageSize:8,
    realname:'',
    gender:'',
    beginDate:'',
    endDate:''
  }
  realName.value=''
  gender.value=''
  dateTime.value=''
})

const dateFormat=(()=>{
  for (let i = 0; i < tableData.value.length; i++) {
    if (tableData.value[i].birthday){
      tableData.value[i].birthday=moment(tableData.value[i].birthday).format('YYYY-MM-DD')
    }
  }
})

const selectable = (row, index) => {
  // 在这里可以设置某些条件下的行是否可选
  if (row.username===userStore.user.username){
    return false;
  }
  return true;
};

const saveClientClass=ref('accountInfo')
const rules = {
  username: [
    { required: true, message: '请输入账号', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
  ],
  confPassword: [
    { required: true, message: '请再次输入密码', trigger: 'blur' },
  ]
};

//新增///////////////////
const showSaveClient=ref(false)
const clientForm=ref({
  username: '',
  password:'',
  nickname: '',
  realname: '',
  avatar:'',
  gender: '',
  email: '',
  phone: '',
  birthday: null,
  address: '',
  usertype:''
})
const confPassword=ref('')

//清空来访者表单数据
const cleanClientForm=(()=>{
  clientForm.value={
    username: '',
    password:'',
    nickname: '',
    realname: '',
    avatar:'',
    gender: '',
    email: '',
    phone: '',
    birthday: null,
    address: '',
    usertype:''
  }
  confPassword.value=''
  imageUrl.value=''
})


//设置修改或新增的标识
const sou=ref(false)//false是新增，true是修改
const saveClient=(async ()=>{
  showSaveClient.value=true
  if (!confPassword.value&&!clientForm.value.password&&!clientForm.value.username){
    ElMessage.warning("请填写完整")
  }else if (confPassword.value!==clientForm.value.password){
    ElMessage.warning("两次密码输入不一致")
  }else {
    clientForm.value.usertype=userType.value
    console.log(clientForm.value)
    let res=await userRegisterService(clientForm.value)
    if (res.code===200){
      await getClient()
      ElMessage.success("新增成功")
      showSaveClient.value=false
      cleanClientForm()
    }else {
      ElMessage.warning(res.errMsg)
    }
  }
})


//给选中的数据赋值
const handleUpdate=((row)=>{
  console.log(row)
  clientForm.value=conFix(row)
  imageUrl.value=row.avatar

  confPassword.value=row.password
  console.log(clientForm.value)
})
const conFix=((row)=>{
  //审核状态:  0是未申请 1是已通过 2是已申请 3是已拒绝
  if (row.usertype=="counselor"){
    if (row.status == "未申请") {
      row.status = "0";
    } else if (row.status == "已入驻") {
      row.status = "1";
    } else if (row.status == "已通过") {
      row.status = "2";
    }else {
      row.status = "3";
    }
  }else {
    row.status="0"
  }
  return row;
})
const handleClientEdit=(async ()=>{

  const res=await updateClientService(clientForm.value)
  if (res.code===200){
    await getClient()
    ElMessage.success(res.message)
    showSaveClient.value=false
    cleanClientForm()
  }else {
    ElMessage.warning(res.errMsg)
  }

})

const saveOrUpdate=(()=>{
  if (sou.value){

    handleClientEdit()
  }else{
    saveClient()
  }
})



//删除客户数据
const selected=ref([])
function selectClient(selectedRows) {
  console.log(selectedRows)
  selected.value=selectedRows
}

const deleteClientRows=(async ()=>{
  console.log(selected.value)
  if (selected.value.length===0){
    ElMessage.warning("请先选择数据")
  }else {

    let res=await deleteClientService(selected.value)
    console.log(res)
    if (res.code===200){
      ElMessage.success(res.message)
      await getClient()
      selected.value=[]
    }else {
      ElMessage.warning(res.errMsg)
    }
  }

})

const confDelete=(()=>{
  if (selected.value.length===0){
    ElMessage.warning("请先选择数据")
  }else {
    ElMessageBox.confirm(
      '您确定要删除吗？',
      '删除确认',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    ).then(() => {

      // 执行删除操作
      deleteClientRows()

    }).catch((err) => {
      console.log(err)
    });
  }
})


const handleClientDelete=(async (row)=>{
  selected.value.push(row)
  //console.log(selected.value)
  //
  ElMessageBox.confirm(
    '您确定要删除吗？',
    '删除确认',
    {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }
  ).then(() => {

    // 执行删除操作
    deleteClientRows()

  }).catch((err) => {
    console.log(err)
  });
  //confDialog.value=true
})

//公用 ：删除确认框
const confDeleteDialog=(()=>{

  confDialog.value = false
})









//咨询师审核
const examineImageUrl=ref('')
const examine=(async (row)=>{
  //赋值基本信息
  userForm.value={...row}
  examineImageUrl.value=row.avatar
  console.log(examineImageUrl.value)
  //赋值咨询师信息
  const conRes=await getByUserIdService(row.userid)
  if (conRes.code===200){
    counselorForm.value={...conRes.data}
    //赋值工作时间
    timeRows.value=conRes.data.counselorscheduleList
  }

  showExamine.value=true
  if (timeRows.value.length!=0){
    timeRows.value.forEach(item=>{
      item.endtime=item.endtime.toString().split(' ')[3]
      item.starttime=item.starttime.toString().split(' ')[3]
    })
  }

})

const showExamine=ref(false)
const isDisabled=ref(true)
const examineClass=ref('1')

//表单数据
const counselorForm=ref({
  username:'',
  specialty:'',
  bio:'',
  status:'1',
  counselorscheduleList:[]
})

const userForm=ref({
  nickname:'',
  realname:'',
  gender:'',
  avatar:'',
  email:'',
  phone:'',
  birthday:'',
  address:'',
  status:'2'
})

const changeExamine=(()=>{

})

const cleanExamine=(()=>{
  counselorForm.value= {
    username: '',
    specialty: '',
    bio: '',
    status: '1',
    counselorscheduleList: []
  }
  userForm.value={
    nickname:'',
    realname:'',
    gender:'',
    avatar:'',
    email:'',
    phone:'',
    birthday:'',
    address:'',
    status:'2'
  }
  timeRows.value=[]
  examineImageUrl.value=''
})

const timeRows = ref([
]);

const addRow = () => {
  timeRows.value.push({
    room: '',
    weekend: '',
    starttime: '',
    endtime:''
  });
};

const deleteRow = (index: number) => {
  timeRows.value.splice(index, 1);
};


const drawer=ref(false)

const filedForm=ref({
   username:'',
   approvalresult:'',
   approvalnote:'',
   approver:'',
   updatedat:''
})

const cleanFiledForm=(()=>{
  filedForm.value={
    username:'',
    approvalresult:'',
    approvalnote:'',
    approver:'',
    updatedat:''
  }
})

//弹出审批单子
const filed=((username)=>{
  drawer.value=true

  filedForm.value.approver=userStore.user.nickname
  filedForm.value.updatedat=new Date()
  filedForm.value.username=username
  console.log(filedForm.value)
})


import {approvalService} from '@/api/filed.js'
//提交审批
const approval=(async ()=>{
  filedForm.value.updatedat=new Date()
  if (filedForm.value.approvalresult){
    console.log(filedForm.value)
    const res=await approvalService(filedForm.value)
    if (res.code===200){
      ElMessage.success(res.message)

      //刷新视图
      drawer.value=false
      showExamine.value=false
      await getClient()
    }else {
      ElMessage.warning(res.errMsg)
    }

  }else {
    ElMessage.warning("请填写审批结果")
  }
})


</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 110px;
  height: 110px;
  text-align: center;
}
.demo-date-picker {
  display: flex;
  padding: 0;
  flex-wrap: wrap;
}
.demo-date-picker .block {
  padding: 30px 0;
  text-align: center;
  border-right: solid 1px var(--el-border-color);
  flex: 0;
}
.demo-date-picker .block:last-child {
  border-right: none;
}
.demo-date-picker .demonstration {
  display: block;
  color: var(--el-text-color-secondary);
  font-size: 14px;
  margin-bottom: 20px;
}

.demo-pagination-block + .demo-pagination-block {
  margin-top: 10px;
}
.demo-pagination-block .demonstration {
  margin-bottom: 16px;
}
.awsCenter{
  display: flex;
  align-items: center;
}
</style>
